/*
 * AlbumStackController.fx
 *
 * Created on 9-jun-2009, 15:00:08
 */

package fotoboek.controls;
import fotoboek.Main;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.layout.VBox;
import fotoboek.controls.*;
import fotoboek.assets.stubs.albumTileUI;
import fotoboek.assets.AlbumBar;
import fotoboek.controls.NavigationMixin;
import fotoboek.model.model.Album;

/**
 * @author r.mohammadi
 */

public class AlbumStackController extends CustomNode, NavigationMixin {
// Containers, for the albums and photos, Items will be added to these containers in runtime
    def albumStack:VBox = VBox {
        spacing:Main.config.ALBUM_STACK_SPACING
        layoutY: Main.config.ALBUM_STACK_TRANSLATE_Y
        translateY: bind ey
    }
    public var clicked : function():Void;
    public var albumRef : AlbumBarController[];
    
    
    override var selectedIndex = -1 on replace oldValue{
        println("index {selectedIndex}");
               // when selected album is < 0 or > the max index, then reset to the last or first album
            if (selectedIndex < 0) then selectedIndex = sizeof(albumRef)-1; if(selectedIndex > sizeof(albumRef)-1) then selectedIndex = 0;
                // when selected album is between the index bounds
                if(selectedIndex >= 0 and selectedIndex <= sizeof(albumRef)-1){
                    //set the correct bar as selected and set the correct url to point to the albums xml file
                    albumRef[selectedIndex].albumBar.isPressed = true;
                    albumRef[oldValue].albumBar.isPressed = false;
                    Main.urlFotoBoek = albumRef[selectedIndex].albumBar.album.url;
                    
                    var height = 320;//boundsInParent.height;
                    var maxY = albumRef[selectedIndex].boundsInParent.maxY;
                    var minY = albumRef[selectedIndex].boundsInParent.minY;
                    println("url {Main.urlFotoBoek} maxy {maxY} height {height} inY {minY} ");
                    if( maxY > height or minY< 0){
                        doScroll(height, maxY, minY);
                    }

                    //perform scroll when needed
//                    if(albumRef[selectedAlbum].albumBar_normal.localToScene(albumRef[selectedAlbum].albumBar_normal.boundsInLocal).maxY > (config.SCENE_HEIGHT - config.CORRECTION_SPACE) or
//                    albumRef[selectedAlbum].albumBar_normal.localToScene(albumRef[selectedAlbum].albumBar_normal.boundsInLocal).minY < 0){
//                        Main.doScroll();
//                    }
                }
        };

    public function update(albums : Album[]):Void{
        delete albumStack.content;
        for(album in albums){
               album.id = indexof album;
               def albumBarUI = albumTileUI{};
               var al = AlbumBarController{
                            action: function ():Void{
                                selectedIndex = album.id;
                                clicked;
                            }
                        };
                al.update(album);
                //Insert the AlbumBar node into the VBox albumStack,
                //by appending it to the contents sequence.
                insert al into albumStack.content;
                insert al into albumRef;
        }
        //Main.loadComplete = Main.albumLoadComplete = true;
       // Main.showScreen("home","album");
    }

     public override function create(): Node {
         return albumStack
     }

 }


